package com.rp.api;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.rp.news.common.PagedResult;
import com.rp.news.util.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseController {

    @Autowired
    public RedisOperator redis;


    @Autowired
    public RestTemplate restTemplate;


    public static final String SMS_CODE = "sms:code";
    public static final String REDIS_USER_TOKEN = "redis_user_token";
    public static final String REDIS_USER_INFO = "redis_user_info";
    public static final Integer COOKIE_MONTH = 30 * 24 * 60 * 60;
    public static final Integer COOKIE_DELETE = 0;
    public static final Integer COMMON_START_PAGE = 1;
    public static final Integer COMMON_PAGE_SIZE= 10;


    public static final String REDIS_ADMIN_TOKEN = "redis_admin_token";
    public static final String REDIS_ALL_CATEGORY = "redis_all_category";


    public static final String REDIS_ALREADY_READ = "redis_already_read:";
    public static final String REDIS_ARITCLE_READ_COUNTS = "redis_aritcle_read_counts:";




    @Value("${website.domain-name}")
    private String domainName;


    public Map<String,String> getErrors(BindingResult bindResult){
        Map<String,String> map = new HashMap<>();
        List<FieldError> fieldErrors = bindResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }
        return map;
    }

    public void setCookie(HttpServletRequest request,
                          HttpServletResponse response,
                          String cookieName, String cookieValue, Integer maxAge){

        String encode = null;
        try {
            encode = URLEncoder.encode(cookieValue, "UTF-8");
            setCookieValue(request,response,cookieName, maxAge, encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    private void setCookieValue(HttpServletRequest request,
                                HttpServletResponse response,String cookieName, Integer maxAge, String encode) {
        Cookie cookie = new Cookie(cookieName,encode);
        cookie.setDomain(domainName);
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        response.addCookie(cookie);

    }

    public  void deleteCookie(HttpServletRequest request, HttpServletResponse response,String cookieName){
        String encode = null;
        try {
            encode = URLEncoder.encode("", "utf-8");
            setCookie(request,response,cookieName,encode,COOKIE_DELETE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public PagedResult setterPageResult(List<?> list, Integer page){
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedResult result = new PagedResult();
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }


    public Integer getCountsFromRedis(String key){
        String s = redis.get(key);

        if(StrUtil.isBlank(s)){
            return 0;
        }

        return Integer.valueOf(s);
    }
}
